// LuoSnapShot.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"

#include "ToolFrame.h"
#include "WebFrame.h"
#include "Clipboard.h"
#include "ActivatorImpl.h"

#ifdef _MANAGED
#pragma managed(push, off)
#endif

HINSTANCE g_hDLL;
HHOOK g_hHook = 0;
HHOOK g_hHookLL = 0;

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call) {
	case DLL_PROCESS_ATTACH:
		g_hDLL = hModule;
		return TRUE;
	case DLL_PROCESS_DETACH:
		if (g_hHook != 0) {
			BOOL b = UnhookWindowsHookEx(g_hHook);
			g_hHook = 0;
		}

		if (g_hHookLL != 0) {
			BOOL b = UnhookWindowsHookEx(g_hHookLL);
			if (b) {
				OutputDebugStringW(L"LuoSnapShot : Uninstall Hook OK.\n");
			}

			g_hHookLL = 0;
		}

		return TRUE;
	}

    return TRUE;
}

// LuoSnapShotModule
class LuoSnapShotModule :
	public ATL::CAtlDllModuleT<LuoSnapShotModule> {
};

// LuoSnapShotModule
LuoSnapShotModule _AtlModule;

static DWORD s_tid;

static LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
	static DWORD f_tid;
	DWORD tid = GetCurrentThreadId();
	if (f_tid != tid) {
		ATLTRACE("LuoSnapShot : HookProc(%d : %d)\n", tid, s_tid);
		f_tid = tid;
	}

	if (nCode < 0) {
		return CallNextHookEx(g_hHook, nCode, wParam, lParam);
	}

#if 0
	int code = (int)wParam;
	if (code == VK_SNAPSHOT) {
		WCHAR buf[128];
		wsprintf(buf, L"VK_SNAPSHOT : (%d) %d - %08X\n", nCode, code, lParam);
		OutputDebugStringW(buf);
	}
#endif

	return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}

/* */
static LRESULT CALLBACK HookProcLL(int nCode, WPARAM wParam, LPARAM lParam)
{
	static DWORD f_tid;
	DWORD tid = GetCurrentThreadId();
	if (f_tid != tid) {
		ATLTRACE("LuoSnapShot : HookProcLL(%d : %d)\n", tid, s_tid);
		f_tid = tid;
	}

	if (nCode < 0) {
		return CallNextHookEx(g_hHook, nCode, wParam, lParam);
	}

#if 0
	if (nCode == HC_ACTION) {
		KBDLLHOOKSTRUCT*  key = (KBDLLHOOKSTRUCT*)lParam;
		if (key->vkCode == VK_SNAPSHOT) {
			LPCWSTR kn = L"";

			switch (wParam) {
			case WM_KEYDOWN:
				kn = L"WM_KEYDOWN";
				break;
			case WM_KEYUP:
				kn = L"WM_KEYUP";
				break;
			case WM_SYSKEYDOWN:
				kn = L"WM_SYSKEYDOWN";
				break;
			case WM_SYSKEYUP:
				kn = L"WM_SYSKEYUP";
				break;
			}

			WCHAR buf[128];
			wsprintf(buf, L"VK_SNAPSHOT : %s(%d) %d - %08X\n", kn, wParam, key->vkCode, key->flags);
			OutputDebugStringW(buf);
		}
	}
#endif

	return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}

/* */
void* UActivator_Create()
{
	static ActivatorImpl impl;

	if (s_tid == 0) {
		s_tid = GetCurrentThreadId();
		ATLTRACE("LuoSnapShot : tid(%d)\n", s_tid);
	}

#if 0
	if (g_hHook == 0) {
		HHOOK hHook = SetWindowsHookEx(WH_KEYBOARD, HookProc, g_hDLL, 0);
		if (hHook != 0) {
			g_hHook = hHook;
		}
	}

	if (g_hHookLL == 0) {
		HHOOK hHook = SetWindowsHookEx(WH_KEYBOARD_LL, HookProcLL, g_hDLL, 0);
		if (hHook != 0) {
			OutputDebugStringW(L"LuoSnapShot : Install Hook OK.\n");
			g_hHookLL = hHook;
		}
	}
#endif
	return static_cast<LuoSnapShot::UActivator*>(&impl);
}

extern "C" HRESULT LUOSNAPSHOT_FUNC STDCALL LuoSnapShot_Activate(LPCWSTR token, void ** obj)
{
	if (obj == 0) {
		return E_INVALIDARG;
	}

	*obj = 0;

	if (token == 0) {
		return E_INVALIDARG;
	}

	void*  new_obj = 0;

	if (lstrcmpW(token, L"activator") == 0) {
		new_obj = UActivator_Create();
	}

	if (new_obj == 0) {
		return E_NOINTERFACE;
	}

	*obj = new_obj;
	return S_OK;
}


#ifdef _MANAGED
#pragma managed(pop)
#endif

